import pandas as pd
import matplotlib.pyplot as plt
import random
from datetime import datetime, timedelta

# 生成时间戳（每天4个数据点，每6小时一个数据点）
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 12, 31)
num_days = (end_date - start_date).days + 1  # 总天数
timestamps = []

for i in range(num_days):
    for j in range(4):
        timestamps.append(start_date + timedelta(days=i, hours=j*6))

# 生成季节性海洋温度数据和风速数据（参考日本地区气候特点）
temperature = []
wind_speed = []

last_temperature = random.uniform(10, 20)  # 初始化温度为春季的初始温度
last_wind_speed = random.uniform(1, 5)  # 初始化风速在1到5之间
for timestamp in timestamps:
    month = timestamp.month

    # 根据月份模拟季节性温度变化（以日本地区为例）
    if 3 <= month <= 5:  # 春季
        temperature.append(last_temperature)
        wind_speed.append(last_wind_speed)
        last_temperature += random.uniform(-1, 1)  # 使温度变化平滑
        last_wind_speed += random.uniform(-0.01, 0.01)  # 使风速变化平滑
    elif 6 <= month <= 8:  # 夏季
        temperature.append(last_temperature)
        wind_speed.append(last_wind_speed)
        last_temperature += random.uniform(-1, 1)  # 使温度变化平滑
        last_wind_speed += random.uniform(-0.01, 0.01)  # 使风速变化平滑
    elif 9 <= month <= 11:  # 秋季
        temperature.append(last_temperature)
        wind_speed.append(last_wind_speed)
        last_temperature += random.uniform(-1, 1)  # 使温度变化平滑
        last_wind_speed += random.uniform(-0.01, 0.01)  # 使风速变化平滑
    else:  # 冬季
        temperature.append(last_temperature)
        wind_speed.append(last_wind_speed)
        last_temperature += random.uniform(-1, 1)  # 使温度变化平滑
        last_wind_speed += random.uniform(-0.01, 0.01)  # 使风速变化平滑

# 创建数据框
data = pd.DataFrame({
    'timestamp': timestamps,
    'temperature': temperature,
    'wind_speed': wind_speed
})

# 保存数据到 CSV 文件
data.to_csv('data_resource/environmental_data.csv', index=False)
# 读取CSV文件
data = pd.read_csv('data_resource\environmental_data.csv')
# 转换时间列为日期时间格式
data['timestamp'] = pd.to_datetime(data['timestamp'])
# 绘制海洋温度随时间的变化曲线
plt.figure(figsize=(10, 6))
plt.plot(data['timestamp'], data['temperature'], color='blue', label='Ocean temperature')
plt.xlabel('time')
plt.ylabel('temperature')
plt.title('Ocean temperature change')
plt.xticks(rotation=45) 
plt.legend()
plt.grid(True)
plt.show()
# 绘制风速随时间的变化曲线
plt.figure(figsize=(10, 6))
plt.plot(data['timestamp'], data['wind_speed'], color='green', label='Wind speed')
# plt.scatter(data['timestamp'], data['wind_speed'], color='green', label='Wind speed')
plt.xlabel('time')
plt.ylabel('Wind speed')
plt.title('Wind speed change')
plt.xticks(rotation=45)
plt.legend()
plt.grid(True)
plt.show()